import { ipcMain, IpcMainEvent, IpcMainInvokeEvent } from "electron";
import { createWindow, getTargetWindow } from "../window/CreateWindow";
import { IcreateWindowParams, windowTabType } from "../types/window";
import { createChildWindow } from "../window/CreateChildWindow";

export function WindowEvent() {
  // 窗口操作
  ipcMain.on("controlTabBar", (_event: IpcMainEvent, params: windowTabType) => {
    let targetWindow = getTargetWindow(params.obj);
    if (!targetWindow) return;
    switch (params.type) {
      case "close":
        targetWindow.close();
        break;
      case "mini":
        targetWindow.minimize();
        break;
      case "full":
        targetWindow.isMaximized() ? targetWindow.unmaximize() : targetWindow.maximize();
        break;
    }
  });

  // 创建窗口
  ipcMain.handle("createWindow", (_event: IpcMainInvokeEvent, params: IcreateWindowParams): boolean => {
    let targetWindow = getTargetWindow(params.title);
    if (targetWindow) targetWindow.close();
    return createWindow(params.path, params.width, params.height, params.title);
  });

  // 创建子窗口
  ipcMain.on("createChildWindow", (_event: IpcMainEvent, params: IcreateWindowParams) => createChildWindow(params));
}
